package cn.kgc.crowdfundingadminspringboot.service;

import cn.kgc.crowdfundingadminspringboot.mapper.AdvertisementMapper;
import cn.kgc.crowdfundingadminspringboot.pojo.Advertisement;
import cn.kgc.crowdfundingadminspringboot.pojo.AdvertisementExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("advertisementService")
public class AdvertisementServiceImpl implements  AdvertisementService{

    @Resource
    AdvertisementMapper advertisementMapper;


    @Override
    public PageInfo<Advertisement> select(Integer pageNum, Integer pageSize, String keyword) {
        // 1.开启分页功能
        PageHelper.startPage(pageNum, pageSize);

        // 2.执行查询
        AdvertisementExample advertisementExample=new AdvertisementExample();
        AdvertisementExample.Criteria criteria = advertisementExample.createCriteria();
        criteria.andNameLike("%"+keyword+"%");
        List<Advertisement> list = advertisementMapper.selectByExample(advertisementExample);
        // 3.封装为PageInfo对象
        return new PageInfo<>(list);
    }

    @Override
    public List<Advertisement> getCertListByIdList(List<Integer> advertisementIdList) {
        AdvertisementExample advertisementExample=new AdvertisementExample();

        // 在Example对象中封装查询条件
        advertisementExample.createCriteria().andIdIn(advertisementIdList);

        // 执行查询
        return advertisementMapper.selectByExample(advertisementExample);
    }

    @Override
    public void batchRemove(List<Integer> advertisementIdList) {
        AdvertisementExample advertisementExample=new AdvertisementExample();

        advertisementExample.createCriteria().andIdIn(advertisementIdList);

        advertisementMapper.deleteByExample(advertisementExample);
    }

    @Override
    public void saveAdvertisement(String advertisementName) {
        advertisementMapper.insert(new Advertisement(null, advertisementName));
    }

    @Override
    public void updateAdvertisement(Advertisement advertisement) {
        advertisementMapper.updateByPrimaryKey(advertisement);
    }
}
